﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class MessageToUserService
    {
        DBHelper db = new DBHelper();
        MessageService messageService = new MessageService();
        UserInfoService userInfoService = new UserInfoService();
        /// <summary>
        /// 根据messageId、toUserId修改MessageToUser
        /// </summary>
        /// <param name="messageToUser"></param>
        /// <returns>修改结果1或0</returns>
        public int updateMessageToUserByMessageIdAndToUserId(MessageToUser messageToUser)
        {
            SqlParameter[] para = new SqlParameter[]{
               new SqlParameter("@ifRead",messageToUser.IfRead),
               new SqlParameter("@messageId",messageToUser.Message.MessageId),
               new SqlParameter("@toUserId",messageToUser.ToUser.UserId),
               new SqlParameter("@ifRecycle",messageToUser.IfRecycle)
           };
            int result = db.getCount("proc_updateMessageToUserByMessageIdAndToUserId", para);
            db.CloseConnection();
            return result;
        }
        /// <summary>
        /// 向表MessageToUser中插入新信息
        /// </summary>
        /// <param name="messageToUser"></param>
        /// <returns>插入结果1或0</returns>
        public int insertMessageToUser(MessageToUser messageToUser)
        {
            SqlParameter[] para = new SqlParameter[]{
               new SqlParameter("@ifRead",messageToUser.IfRead),
               new SqlParameter("@messageId",messageToUser.Message.MessageId),
               new SqlParameter("@toUserId",messageToUser.ToUser.UserId),
               new SqlParameter("@ifRecycle",messageToUser.IfRecycle)
           };
            int result = db.getCount("proc_insertMessageToUser", para);
            db.CloseConnection();
            return result;
        }
        /// <summary>
        /// 根据messageId、toUserId查找MessageToUser
        /// </summary>
        /// <param name="toUserId">用户Id</param>
        /// <param name="messageId">信息Id</param>
        /// <returns>查找到的信息</returns>
        public MessageToUser getMessageToUserByMessageIdAndToUserId(int toUserId, int messageId)
        {
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@toUserId",toUserId),
                new SqlParameter("@messageId",messageId)};
            return GetMessageToUserByPara("proc_getMessageToUserByMessageIdAndToUserId", para);
        }
        /// <summary>
        /// 根据messageId查找MessageToUser
        /// </summary>
        /// <param name="messageId">要查找的信息Id</param>
        /// <returns></returns>
        public MessageToUser GetMessageToUserByMessageId(int messageId)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@messageId", messageId) };
            return GetMessageToUserByPara("proc_getMessageToUserByMessageId", para);

        }
        /// <summary>
        /// 根据messageId查找所有MessageToUser
        /// </summary>
        /// <param name="messageId">要查找的信息Id</param>
        /// <returns>返回MessageToUser集合</returns>
        public IList<MessageToUser> getAllMessageToUserByMessageId(int messageId)
        {
            try
            {
                MessageToUser messageToUser=null;
                List<MessageToUser> messageToUserList = new List<MessageToUser>();
                SqlDataReader reader = db.getDataReader("proc_getAllMessageToUserByMessageId", new SqlParameter("@messageId", messageId));
                while(reader.Read())
                {
                    messageToUser = new MessageToUser();
                    messageToUser.Id = (int)reader["Id"];
                    messageToUser.IfRead = (int)reader["IfRead"];
                    messageToUser.IfRecycle = (int)reader["IfRecycle"];
                    int messageID = (int)reader["messageId"];
                    int toUserID = (int)reader["toUserId"];
                    messageToUser.Message = messageService.getMessageByMessageId(messageID);
                    messageToUser.ToUser = userInfoService.GetUserInfoByUserId(toUserID);
                    messageToUserList.Add(messageToUser);
                }
                reader.Close();
                db.CloseConnection();
                return messageToUserList;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        /// <summary>
        /// 根据参数返回MessageToUser
        /// </summary>
        /// <param name="proc"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        private MessageToUser GetMessageToUserByPara(string proc, SqlParameter[] para)
        {
            try
            {
                MessageToUser messageToUser = new MessageToUser();

                SqlDataReader reader = db.getDataReader(proc, para);
                if (reader.Read())
                {
                    messageToUser.Id = (int)reader["Id"];
                    messageToUser.IfRead = (int)reader["IfRead"];
                    messageToUser.IfRecycle = (int)reader["IfRecycle"];
                    int messageID = (int)reader["messageId"];
                    int toUserID = (int)reader["toUserId"];
                    messageToUser.Message = messageService.getMessageByMessageId(messageID);
                    messageToUser.ToUser = userInfoService.GetUserInfoByUserId(toUserID);
                }
                reader.Close();
                db.CloseConnection();
                return messageToUser;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        /// <summary>
        /// 根据messageId、toUserId删除MessageToUser
        /// </summary>
        /// <param name="toUserId">用户Id</param>
        /// <param name="messageId">信息Id</param>
        /// <returns>删除结果1或0</returns>
        public int deleteMessageByMessageIdAndToUserId(int toUserId, int messageId)
        {
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@messageId",messageId),
                new SqlParameter("@toUserId",toUserId)
            };
            int result = db.getCount("proc_deleteMessageToUserByMessageIdAndToUserId", para);
            db.CloseConnection();
            return result;
        }
        /// <summary>
        /// 根据messageId删除MessageToUser
        /// </summary>
        /// <param name="messageId">要删除的messageId</param>
        /// <returns>删除结果1或0</returns>
        public int deleteMessageToUserByMessageId(int messageId)
        {
            int result = db.getCount("proc_deleteMessageToUserByMessageId", new SqlParameter("@messageId", messageId));
            db.CloseConnection();
            return result;
        }
    }
}
